import axios from 'axios'
import router from '../../router'

// axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8'
// axios.defaults.headers['Content-Type'] = 'multipart/form-data'
const baseUrlWei = 'https://122.112.236.124:443'
// 122.112.236.124:443
axios.defaults.timeout = 10000
const instance = axios.create()
instance.interceptors.request.use((config) => {
  config.headers['Content-Type'] = 'multipart/form-data'
  return config
}, (error) => {
  return Promise.reject(error)
})

instance.interceptors.response.use((res) => {
  let isLogin = res.headers.islogin
  if (isLogin === 'true') {
    return res
  } else {
    alert('登录验证过期')
    router.push('/login')
    return res
  }
}, (error) => {
  return Promise.reject(error)
})

export function fetch (url, params) {
  return new Promise((resolve, reject) => {
    instance.post(url, params)
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

export default {
  // 增加商家
  addBusiness (params) {
    return fetch(`${baseUrlWei}/airplane/interface/business_manage/add_business`, params)
  },
  // 增加商家
  editBusiness (params) {
    return fetch(`${baseUrlWei}/airplane/interface/business_manage/update_business`, params)
  },
  // 批量导入商家
  addImportBusiness (params) {
    return fetch(`${baseUrlWei}/airplane/interface/business_manage/import_business`, params)
  },
  // 批量导入航班信息
  addImportAirplane  (params) {
    return fetch(`${baseUrlWei}/airplane/interface/flight_info/update_info`, params)
  }
}
